package com.formula.datastructure.question.stack;

import com.formula.datastructure.standard.stack.Stack;

public class Bracket {

    private char[] str;

    public Bracket(String input) {
        str = input.toCharArray();
    }

    // 这种方法是定义了一个stack类
    public boolean isValid() {
        Stack<Character> stack = new Stack();
        for (int i = 0; i < str.length; i++) {
            if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
                stack.push(str[i]);
            }
            if (str[i] == ')' && stack.getTop().equals('(')) {
                stack.pop();
            }
            if (str[i] == ']' && stack.getTop().equals('[')) {
                stack.pop();
            }
            if (str[i] == '}' && stack.getTop().equals('{')) {
                stack.pop();
            }
        }
        return stack.isEmpty();
    }

    // 这种方法直接用数组
    public int match(char[] expression) {
        char[] stack;
        int top = -1;

        /**
         * 记住思路:
         * 看到左边括号, 就push
         * 看到右边括号, 就看当前的栈顶是否是对应的左边括号, 如果是, 就pop
         */
        for (int i = 0; i < expression.length; i++) {

        }
        return 0;
    }

    public static void main(String[] args) {
        String input = "[(2+5)*3+4*[(8+9)]]";
        Bracket bracket = new Bracket(input);
        System.out.println(bracket.isValid());
        input = "[(2+5)*[3)+(4]*[(8+9)]]";
        bracket = new Bracket(input);
        System.out.println(bracket.isValid());
    }
}
